﻿using System;
using System.Windows;
using System.Collections.ObjectModel;
using System.Windows.Media;
using System.Windows.Controls;
using System.Windows.Input;

namespace QueryResource.ViewModel
{
    public class RibbonViewModel : DependencyObject
    {
        public static readonly DependencyProperty CategoriesProperty =
            DependencyProperty.Register("Categories", typeof(ObservableCollection<RibbonCategoryViewModel>), typeof(RibbonViewModel), new PropertyMetadata(null));
        public static readonly DependencyProperty MenuItemsProperty =
            DependencyProperty.Register("MenuItems", typeof(ObservableCollection<RibbonCommandViewModel>), typeof(RibbonViewModel), new PropertyMetadata(null));
        public ObservableCollection<RibbonCategoryViewModel> Categories
        {
            get { return (ObservableCollection<RibbonCategoryViewModel>)GetValue(CategoriesProperty); }
            set { SetValue(CategoriesProperty, value); }
        }
        public ObservableCollection<RibbonCommandViewModel> MenuItems
        {
            get { return (ObservableCollection<RibbonCommandViewModel>)GetValue(MenuItemsProperty); }
            set { SetValue(MenuItemsProperty, value); }
        }
        public RibbonViewModel()
        {
            Categories = new ObservableCollection<RibbonCategoryViewModel>();
            MenuItems = new ObservableCollection<RibbonCommandViewModel>();
        }
        public void Clear()
        {
            foreach (RibbonCategoryViewModel cat in Categories)
            {
                cat.Clear();
            }
            Categories.Clear();
        }
    }
    public class RibbonCategoryViewModel : RibbonViewModelBase
    {
        public static readonly DependencyProperty PagesProperty =
    DependencyProperty.Register("Pages", typeof(ObservableCollection<RibbonPageViewModel>), typeof(RibbonCategoryViewModel), new PropertyMetadata(null));

        public ObservableCollection<RibbonPageViewModel> Pages
        {
            get { return (ObservableCollection<RibbonPageViewModel>)GetValue(PagesProperty); }
            set { SetValue(PagesProperty, value); }
        }

        public RibbonCategoryViewModel()
        {
            Pages = new ObservableCollection<RibbonPageViewModel>();
        }
        public void Clear()
        {
            foreach (RibbonPageViewModel cat in Pages)
            {
                cat.Clear();
            }
            Pages.Clear();
        }
    }
    public class RibbonPageViewModel : RibbonViewModelBase
    {
        public static readonly DependencyProperty GroupsProperty;

        static RibbonPageViewModel()
        {
            GroupsProperty = DependencyProperty.Register("Groups", typeof(ObservableCollection<RibbonPageGroupViewModel>), typeof(RibbonPageViewModel), new PropertyMetadata(null));
        }
        public RibbonPageViewModel()
        {
            Groups = new ObservableCollection<RibbonPageGroupViewModel>();
        }
        public ObservableCollection<RibbonPageGroupViewModel> Groups
        {
            get { return ((ObservableCollection<RibbonPageGroupViewModel>)GetValue(GroupsProperty)); }
            set { SetValue(GroupsProperty, value); }
        }
        public void Clear()
        {
            foreach (RibbonPageGroupViewModel cat in Groups)
            {
                cat.Clear();
            }
            Groups.Clear();
        }
    }
    public class RibbonViewModelBase : DependencyObject
    {
        public static readonly DependencyProperty NameProperty;

        static RibbonViewModelBase()
        {
            NameProperty = DependencyProperty.Register("Name", typeof(string), typeof(RibbonViewModelBase), new PropertyMetadata(""));
        }
        public string Name
        {
            get { return (string)GetValue(NameProperty); }
            set { SetValue(NameProperty, value); }
        }
    }
    public class RibbonPageGroupViewModel : RibbonViewModelBase, ICommand
    {
        public static readonly DependencyProperty CommandsProperty;
        public static readonly DependencyProperty GlyphProperty;

        static RibbonPageGroupViewModel()
        {
            CommandsProperty = DependencyProperty.Register("Commands", typeof(ObservableCollection<RibbonCommandViewModel>), typeof(RibbonPageGroupViewModel), new PropertyMetadata(null));
            GlyphProperty = DependencyProperty.Register("Glyph", typeof(ImageSource), typeof(RibbonPageGroupViewModel), new PropertyMetadata(null, OnGlyphPropertyChanged));
        }
        public RibbonPageGroupViewModel()
        {
            Commands = new ObservableCollection<RibbonCommandViewModel>();
        }

        public ObservableCollection<RibbonCommandViewModel> Commands
        {
            get { return ((ObservableCollection<RibbonCommandViewModel>)GetValue(CommandsProperty)); }
            set { SetValue(CommandsProperty, value); }
        }
        public ImageSource Glyph
        {
            get { return (ImageSource)GetValue(GlyphProperty); }
            set { SetValue(GlyphProperty, value); }
        }
        protected internal static void OnGlyphPropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            ((RibbonPageGroupViewModel)d).OnGlyphChanged(e);
        }
        protected internal void OnGlyphChanged(DependencyPropertyChangedEventArgs e)
        {
            ((ImageSource)e.NewValue).Freeze();
        }
        public void Clear()
        {
            Commands.Clear();
        }
        #region ICommand

        public bool CanExecute(object parameter)
        {
            return true;
        }

        public event EventHandler CanExecuteChanged;

        public void Execute(object parameter)
        {
            if (GroupClick != null)
            {
                GroupClick();
            }
        }
        #endregion

        public Action GroupClick { get; set; }
    }

    public class RibbonCommandViewModel : DependencyObject, ICommand
    {
        public Action RibbonClick { get; set; }
        public Func<bool> UnRibbonClick { get; set; }
        public static readonly DependencyProperty CaptionProperty;
        public static readonly DependencyProperty LargeGlyphProperty;
        public static readonly DependencyProperty SmallGlyphProperty;

        static RibbonCommandViewModel()
        {
            CaptionProperty = DependencyProperty.Register("Caption", typeof(string), typeof(RibbonCommandViewModel), new PropertyMetadata(""));
            LargeGlyphProperty = DependencyProperty.Register("LargeGlyph", typeof(ImageSource), typeof(RibbonCommandViewModel), new PropertyMetadata(null, OnGlyphPropertyChanged));
            SmallGlyphProperty = DependencyProperty.Register("SmallGlyph", typeof(ImageSource), typeof(RibbonCommandViewModel), new PropertyMetadata(null, OnGlyphPropertyChanged));
        }

        public string Caption
        {
            get { return (string)GetValue(CaptionProperty); }
            set { SetValue(CaptionProperty, value); }
        }
        public ImageSource LargeGlyph
        {
            get { return (ImageSource)GetValue(LargeGlyphProperty); }
            set { SetValue(LargeGlyphProperty, value); }
        }
        public ImageSource SmallGlyph
        {
            get { return (ImageSource)GetValue(SmallGlyphProperty); }
            set { SetValue(SmallGlyphProperty, value); }
        }

        protected internal static void OnGlyphPropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            ((RibbonCommandViewModel)d).OnGlyphChanged(e);
        }
        protected internal void OnGlyphChanged(DependencyPropertyChangedEventArgs e)
        {
            ((ImageSource)e.NewValue).Freeze();
        }
        #region ICommand

        public virtual bool CanExecute(object parameter)
        {
            if (UnRibbonClick != null)
            {
                return UnRibbonClick();
            }

            return true;

        }
        public virtual void Execute(object parameter)
        {
            if (RibbonClick != null)
            {
                RibbonClick();
            }
        }
        public event EventHandler CanExecuteChanged;
        #endregion

        public void RaiseCanExecuteChanged()
        {
            if (this.CanExecuteChanged != null)
            {
                this.CanExecuteChanged(this, EventArgs.Empty);
            }
        }
    }

    public class RibbonCommandTemplateSelector : DataTemplateSelector
    {
        //public override DataTemplate SelectTemplate(object item, DependencyObject container)
        //{
        //    if (item is RibbonSubMenuCommand)
        //        return (DataTemplate)RibbonMainWindow.SharedResources["subItemTemplate"];
        //    return (DataTemplate)RibbonMainWindow.SharedResources["itemTemplate"];
        //}
    }

    public class RibbonSubMenuCommand : RibbonCommandViewModel
    {
        public static readonly DependencyProperty CommandsProperty;

        public ObservableCollection<RibbonCommandViewModel> Commands
        {
            get { return (ObservableCollection<RibbonCommandViewModel>)GetValue(CommandsProperty); }
            set { SetValue(CommandsProperty, value); }
        }
        static RibbonSubMenuCommand()
        {
            CommandsProperty = DependencyProperty.Register("Commands", typeof(ObservableCollection<RibbonCommandViewModel>), typeof(RibbonSubMenuCommand), new PropertyMetadata(null));
        }
        public RibbonSubMenuCommand()
        {
            Commands = new ObservableCollection<RibbonCommandViewModel>();
        }
    }

}
